/*----------------------------------------------------------\\
|| Mancala                                                  ||
|| CS-340 with Professor Troy                               ||
|| Joshua Jung and Eugene Magdel                            ||
|| April 29, 2011                                           ||
\\----------------------------------------------------------*/
#include "cup.h"

Cup * Cup::current=NULL;

Cup::Cup(int index, int _owner) : next(NULL){
    owner = _owner;

    current_index = index;

    gem = last_gem = NULL;

    if(!is_mancala()){
        gem = new Gem();
        last_gem = gem;
        for(int i=1; i<4; ++i){
            last_gem -> next = new Gem();
            last_gem = last_gem -> next;
        }
    }

    if(Cup::current == NULL)
    {
        Cup::current = this;
    }
}

int Cup::get_index(){
    return current_index;
}

//returns cup at given index
Cup& Cup::cup_at(int x){
    Cup * clicked_cup = Cup::current;
    while(clicked_cup -> get_index() != x){
        clicked_cup = clicked_cup -> next;
        //cout << "Need cup " << x << " but is on cup " << clicked_cup -> get_index() << endl;
    }
    return *clicked_cup;
}

int Cup::get_owner(){
    return owner;
}

Cup* Cup::get_next(){
    return next;
}

void Cup::set_next(Cup* next_cup){
    next = next_cup;
}

bool Cup::is_mancala(){
    return current_index % 7 == 0;
}

int Cup::gem_count(){
    int i=0;
    Gem * current_gem = gem;
    while(current_gem != NULL){
        ++i;
        current_gem = current_gem -> next;
    }
    return i;
}

Cup& Cup::get_mancala_for(int player){
    //player 0 has mancala at index 7, player 1 has mancala at index 0
    return Cup::cup_at((player*7+7)%14);
}

Cup& Cup::get_across(){
    return Cup::cup_at(14 - get_index());
}

//Must be Mancala
void Cup::add_gems(Cup & other_cup){
    if(!is_mancala()){
        cout << "Can only be mancala" << endl;
        return;
    }

    if (other_cup.gem_count() == 0)
    {
        return;
    }

    if(last_gem == NULL)
    {
        last_gem = gem = other_cup.gem;
        last_gem = other_cup.last_gem;
        other_cup.last_gem = other_cup.gem = NULL;

        return;
    }
    last_gem -> next = other_cup.gem;
    last_gem = other_cup.last_gem;
    other_cup.last_gem = other_cup.gem = NULL;
    cout << "Added gems to mancala cup" << endl;
    cout << "Other cup has: " << other_cup.gem_count();
}

bool Cup::empty(){
    return gem_count() == 0;
}

void Cup::add_gem(Cup& other_cup){
    if(last_gem == NULL){
        gem = last_gem = other_cup.gem;
    }
    else{
        last_gem -> next = other_cup.gem;
        if(last_gem -> next != NULL){
            last_gem = last_gem -> next;
        }
    }
    other_cup.gem = last_gem -> next;
    if(other_cup.gem == NULL)
        other_cup.last_gem = NULL;
    last_gem -> next = NULL;
}

string Cup::gem_images(){
    string images = "";
    if(empty())
        return images;
    Gem * current_gem = gem;
    while(current_gem != NULL){
        images += (*current_gem).img_src + " ";
        current_gem = current_gem -> next;
    }
    return images;
}

Gem *Cup::getGemAt(int index)
{
    int g = 0;

    Gem *curGem = gem;

    while (g != index)
    {
        curGem = curGem->next;

        g++;
    }

    return curGem;
}
